<template>
    <div class="review-container">
        <!-- 查询表单 -->
        <el-card class="search-card">
            <el-form :inline="true" :model="searchForm">
                <el-form-item label="发布者" prop="userName">
                    <el-input v-model="searchForm.userName" placeholder="用户名" clearable />
                </el-form-item>
                <el-form-item label="内容" prop="content">
                    <el-input v-model="searchForm.content" placeholder="帖子内容" clearable />
                </el-form-item>
                <el-form-item label="状态" prop="status" style="min-width: 200px">
                    <el-select v-model="searchForm.status" placeholder="审核状态" clearable style="width: 100%">
                        <el-option label="待审" :value="0" />
                        <el-option label="通过" :value="1" />
                        <el-option label="拒绝" :value="2" />
                    </el-select>
                </el-form-item>
                <el-form-item label="发布时间">
                    <el-date-picker v-model="searchForm.dateRange" type="daterange" range-separator="至"
                        start-placeholder="开始日期" end-placeholder="结束日期" value-format="YYYY-MM-DD" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="loadData">查询</el-button>
                </el-form-item>
            </el-form>
        </el-card>

        <!-- 内容区域 -->
        <el-scrollbar class="scroll-container">
            <div class="list-container">
                <el-card v-for="post in postList" :key="post.id" class="post-card">
                    <!-- 用户信息 -->
                    <div class="user-info">
                        <el-image :src="getAvatarUrl(post.user.avatar)" class="avatar"
                            :preview-src-list="[getAvatarUrl(post.user.avatar)]"></el-image>
                        <div class="user-details">
                            <div class="nickname">{{ post.user.nickName }}</div>
                            <div class="username">@{{ post.user.userName }}</div>
                            <div class="time">{{ post.createdTime }}</div>
                        </div>
                    </div>

                    <!-- 帖子内容 -->
                    <div class="post-content">{{ post.content }}</div>

                    <!-- 媒体内容 -->
                    <el-scrollbar v-if="post.urls && post.urls.length > 0" class="media-scroll">
                        <div v-if="post.urls && post.urls.length > 0" class="media-grid">
                            <div v-for="(url, index) in post.urls" :key="index" class="media-item">
                                <template v-if="isImage(url)">
                                    <el-image :src="getMediaUrl(url)" class="media-content"
                                        :preview-src-list="post.urls.filter(u => isImage(u)).map(u => getMediaUrl(u))"
                                        :initial-index="index" fit="contain" hide-on-click-modal>
                                        <template #error>
                                            <div class="media-error">
                                                <el-icon>
                                                    <Picture />
                                                </el-icon>
                                                <span>图片加载失败</span>
                                            </div>
                                        </template>
                                    </el-image>
                                </template>

                                <template v-else-if="isVideo(url)">
                                    <video controls class="media-video" :src="getMediaUrl(url)">
                                        <source :src="getMediaUrl(url)" type="video/mp4">
                                    </video>
                                </template>
                            </div>
                        </div>
                    </el-scrollbar>

                    <!-- 审核操作 -->
                    <div class="action-buttons">
                        <el-button type="success" :disabled="post.status !== 0" @click="handleApprove(post.id)">
                            通过
                        </el-button>
                        <el-button type="danger" :disabled="post.status !== 0" @click="handleReject(post.id)">
                            拒绝
                        </el-button>
                        <el-tag :type="statusType[post.status]" class="status-tag">
                            {{ statusText[post.status] }}
                        </el-tag>
                    </div>
                </el-card>
            </div>
        </el-scrollbar>

        <!-- 分页 -->
        <div class="pagination-container">
            <el-pagination v-model:current-page="pagination.current" v-model:page-size="pagination.size"
                :total="pagination.total" :page-sizes="[10, 20, 50]" layout="total, sizes, prev, pager, next" background
                @size-change="loadData" @current-change="loadData" />
        </div>
    </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getPosts, approvePost, rejectPost } from '../api/review'

const BASE_URL = 'http://localhost:8081'

// 状态映射
const statusText = ['待审核', '已通过', '已拒绝']
const statusType = ['warning', 'success', 'danger']

// 响应式数据
const postList = ref([])
const searchForm = reactive({
    userName: '',
    content: '',
    status: null,
    dateRange: []
})
const pagination = reactive({
    current: 1,
    size: 10,
    total: 0
})

// URL处理方法
// 修改URL处理方法
const getMediaUrl = (filename) => {
    return `${BASE_URL}/video/${filename.replace(/^\//, '')}`
}

const getAvatarUrl = (url) => `${BASE_URL}/avatars/${url}`
const getImageUrl = (url) => `${BASE_URL}/avatars/${url}`
const getVideoUrl = (url) => `${BASE_URL}/video/${url}`
const isImage = (url) => /\.(jpg|jpeg|png|gif)$/i.test(url)
const isVideo = (url) => /\.(mp4|webm|ogg)$/i.test(url)

// 加载数据
const loadData = async () => {
    try {
        const offset = (pagination.current - 1) * pagination.size
        const params = {
            offset,
            size: pagination.size,
            userName: searchForm.userName || undefined,
            content: searchForm.content || undefined,
            status: searchForm.status ?? undefined,
            startTime: searchForm.dateRange?.[0] || undefined,
            endTime: searchForm.dateRange?.[1] || undefined
        }

        const res = await getPosts(params)
        postList.value = res.data
        pagination.total = res.total
    } catch (error) {
        ElMessage.error('数据加载失败')
    }
}

// 审核操作
const handleApprove = async (postId) => {
    try {
        await approvePost(postId)
        ElMessage.success('审核通过')
        loadData()
    } catch (error) {
        ElMessage.error('操作失败')
    }
}

const handleReject = async (postId) => {
    try {
        const { value: reason } = await ElMessageBox.prompt('请输入拒绝理由', '拒绝审核', {
            confirmButtonText: '确定',
            cancelButtonText: '取消'
        })
        await rejectPost(postId, reason)
        ElMessage.success('已拒绝')
        loadData()
    } catch (error) {
        if (error !== 'cancel') ElMessage.error('操作失败')
    }
}

// 初始化加载
onMounted(loadData)
</script>

<style scoped>
.review-container {
    height: 100vh;
    display: flex;
    flex-direction: column;
}

.search-card {
    margin-bottom: 10px;
    flex-shrink: 0;
}

.scroll-container {
    flex: 1;
    overflow: hidden;
}

.list-container {
    padding: 10px;
}

.post-card {
    margin-bottom: 20px;
}

.user-info {
    display: flex;
    align-items: center;
    margin-bottom: 15px;
}

.avatar {
    width: 50px;
    height: 50px;
    border-radius: 50%;
    margin-right: 15px;
}

.user-details {
    flex: 1;
}

.nickname {
    font-weight: bold;
    margin-bottom: 3px;
}

.username {
    color: #666;
    font-size: 0.9em;
}

.time {
    color: #999;
    font-size: 0.8em;
}

.post-content {
    margin: 15px 0;
    white-space: pre-wrap;
}

/* 新增媒体网格样式 */
.media-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap: 10px;
    margin: 15px 0;
}

.media-item {
    position: relative;
    aspect-ratio: 1;
    background-color: #f5f5f5;
    border-radius: 4px;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
}

.media-content {
    width: 100%;
    height: 100%;
    object-fit: contain;
    transition: transform 0.3s ease;
}

.media-video {
    max-height: 400px;
    max-width: 100%;
    border-radius: 4px;
}

.media-error {
    display: flex;
    flex-direction: column;
    align-items: center;
    color: #999;
    font-size: 12px;
}

/* 调整操作按钮位置 */
.action-buttons {
    margin-top: 15px;
    display: flex;
    gap: 10px;
    align-items: center;
    border-top: 1px solid #eee;
    padding-top: 15px;
}

.status-tag {
    margin-left: auto;
}

.pagination-container {
    padding: 20px;
    background: white;
    box-shadow: 0 -1px 3px rgba(0, 0, 0, 0.1);
    z-index: 1;
}
</style>